package com.gopay.common.util;

import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Layout;
import org.apache.log4j.Level;
import org.apache.log4j.RollingFileAppender;
import org.apache.log4j.spi.LoggingEvent;
/**
 * 此类存在缺陷，无法处理VerficationCode=[****]的用例
 */
@Deprecated
public class MyConsoleAppender extends ConsoleAppender{

    private static String  FILTERSTR = "VerficationCode=[";
    private static String  REPLACEFILTERSTR = "VerficationCode=\\[";
    @Override
    public void append(LoggingEvent event) {
        try{
            String message =  event.getMessage().toString();
            int start =message.indexOf(FILTERSTR);
            String rep ="";
            String x ="";
            if(start!=-1){
                String str =  message.substring(start);
                int end = str.indexOf("]");
                if(end!=-1){
                    str = str.substring(0,end);
                    rep = str.replaceAll(REPLACEFILTERSTR, "");
                    int length = rep.length();

                    for (int i=0;i<length;i++) {
                        x=x+"*";
                    }
                    rep = REPLACEFILTERSTR+rep;

                    x = FILTERSTR+x;
                }
                message = this.layout.format(event).replaceAll(rep, x);
            }else{
                message = this.layout.format(event);
            }
            if(!checkEntryConditions()) {
                return;
            }
            this.qw.write(message);

            if(layout.ignoresThrowable()) {
                String[] s = event.getThrowableStrRep();
                if (s != null) {
                    int len = s.length;
                    for(int i = 0; i < len; i++) {
                        this.qw.write(s[i]);
                        this.qw.write(Layout.LINE_SEP);
                    }
                }
            }
            if(shouldFlush(event)) {
                this.qw.flush();
            }
        }catch(Exception e){

        }

    }
}
